#include "resolver.h"
#include "version.h"
#include "funcs.h"

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    if (argc < 3) {
        qInfo().noquote() << "\
Usage: <address-to-bind> <port> [options]\n\n\
--no-api\tStart without API (only HTTP version)\n\
--no-http\tStart without web version (only API)\n\
--no-resolve\tOnly meship domains mode\n\
--no-favicon\tWeb page without favicon (-14KB)\n\n\
Mario DNS tool v"+ VERSION + "\n";
        return 0;
    }

    QCoreApplication a(argc, argv);

    // ACII MODERN ART
    qInfo().noquote() <<"\
+---------------------------------------+\n\
|   _ __ ___   __ _ _ __ _  ___         |\n\
|  | '_ ` _ \\ / _` | '__| |/ _ \\        |\n\
|  | | | | | | (_| | |  | | (_) |       |\n\
|  |_| |_| |_|\\__,_|_|_ |_|\\___/     _  |\n\
|      | |           | |            | | |\n\
|    __| |_ __  ___  | |_ ___   ___ | | |\n\
|   / _` | '_ \\/ __| | __/ _ \\ / _ \\| | |\n\
|  | (_| | | | \\__ \\ | || (_) | (_) | | |\n\
|   \\__,_|_| |_|___/  \\__\\___/ \\___/|_| |\n\
|                            ver. " + VERSION + " |\n\
+---------------------------------------+\n\
| https://notabug.org/acetone/mario-dns |\n\
|     " + COPYRIGHT + "      |\n\
+---------------------------------------+\n";

    QString address(argv[1]);
    quint32 port(QString(argv[2]).toInt());

    // INPUT OPTIONS
    bool no_favicon = false;
    bool no_http = false;
    bool no_api = false;
    bool no_resolve = false;

    for (int i = 3; i < argc; ++i)
    {
        std::string param(argv[i]);
        if (param == "--no-favicon") {
            if (no_http) continue;
            qInfo().noquote() << " * Web page favicon disabled";
            no_favicon = true;

        } else if (param == "--no-http") {
            if (no_api) continue;
            qInfo().noquote() << " * Web interface disabled";
            no_http = true;

        } else if (param == "--no-api") {
            if (no_http) continue;
            qInfo().noquote() << " * API disabled";
            no_api = true;

        } else if (param == "--no-resolve") {
            qInfo().noquote() << " * Resolving disabled (only meship)";
            no_resolve = true;
        }
    }

    if (no_favicon or no_http or no_api or no_resolve) {
        qInfo().noquote() << "";
    }

    // INIT OPTIONS AND START RESOLVER
    if (not no_http) {
        funcs::createHtmlPage(no_favicon);
    }

    Resolver server(address, port);

    if (no_http) server.disableWebPage();
    if (no_api) server.disableAPI();
    if (no_resolve) server.disableResolv();

    return a.exec();
}
